1574D - The Strongest Build - CodeForces Solution


binary search brute force data structures dfs and similar graphs greedy hashing implementation *2000

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
#define endl '\n'


// First = hash, index 
// Second = final hash, final result
set<vector<int>> s; // Banned
int n;
const int MAX_LOW = -2e9;

pair<vector<int>, int> dp(vector<vector<int>> &v, vector<int> &hash, int i=0) {
	if (i == n) {
		if (s.find(hash) == s.end())
			return {{}, 0};
		
		else
			return {{}, MAX_LOW};
	}
	
	else {
		pair<vector<int>, int> result = {{}, MAX_LOW};
		
		for (int j = v[i].size()-1; j >= 0; j--) {
			hash.push_back(j+1);
			pair<vector<int>, int> nxtRes = dp(v, hash, i+1);
			
			nxtRes.first.push_back(j+1);
			nxtRes.second += v[i][j];
			
			if (nxtRes.second > result.second)
				result = nxtRes;
			
			if (s.find(hash) == s.end()) {
				hash.pop_back();
				break;
			}
			
			else
				hash.pop_back();
		}
		
		return result;
	}
}


vector<string> removeDupWord(string str)
{
	vector<string> result;
	string cur = "";
	
	for (char c : str) {
		if (c == ' ') {
			result.push_back(cur);
			cur = "";
		}
		
		else
			cur += c;
	}
	
	result.push_back(cur);
	return result;
}


void solve() 
{
	cin >> n;
	vector<vector<int>> v(n);
	
	for (int i = 0; i < n; i++) {
		int m;
		cin >> m;
		v[i] = vector<int>(m);
		
		for (int j = 0; j < m; j++)
			cin >> v[i][j];
	}
	
	int m;
	cin >> m;
	vector<vector<int>> banned(m, vector<int>(n));
	
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			cin >> banned[i][j];
		
	for (int i = 0; i < m; i++) {
		vector<int> cur;
		
		for (int j = 0; j < n; j++) {
			cur.push_back(banned[i][j]);
			s.insert(cur);
		}
	}
	
	vector<int> hash;
	vector<int> res = dp(v, hash).first;
	reverse(res.begin(), res.end());
	
	for (int val : res)
		cout << val << " ";
	
	cout << endl;
}


int main() 
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
//	int t;
//	cin >> t;
//	
//	while (t--)
//		solve();
	
	solve();
}


Comments

Submit
0 Comments
More Questions

1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome